/**
 * Created by Administrator on 2017/11/18.
 */

(function () {
    var codesInfo = [
        {code: 'WAIT_ACCEPT', label: '待受理案件未处理', cssClass: "icon-accept", href: "test"},
        {code: 'WAIT_REGISTER', label: '待立案案件未处理', cssClass: "icon-register"},
        {code: 'WAIT_DISPATCH', label: '待派遣案件未处理', cssClass: "icon-dispatch"},
        {code: 'WAIT_CLOSE', label: '待结案案件未处理', cssClass: "icon-close"},
        {code: 'WAIT_AUDIT_DISPATCH_DELAY', label: '延期申请未处理', cssClass: "icon-postpone"},
        {code: 'WAIT_AUDIT_DISPATCH_LOGOUT', label: '注销申请未处理', cssClass: "icon-logout"}/*,
        {code:'EXCEED_TIME_LIMIT_WAIT_DISPATCH',label:'待派遣案件超期未处理',cssClass:"#F45438"},
        {code:'EXCEED_TIME_LIMIT_WAIT_CLOSE',label:'待结案案件超期未处理',cssClass:"#F29503"},
        {code:'EXCEED_TIME_LIMIT',label:'案件超期',cssClass:"#F45539"}*/
    ];

    var maxNumber = 0;//保持周和月最大刻度一致
    $(function () {
        initCount();
        initMonthChart();
        initYearChart();
    });

    /**
     * 初始化数量统计
     */
    function initCount() {
        post(getPath() + "dossier/status/waitstatics", {}, function (result) {
            //console.log(result);
            var maxLen = 4, lastIndex = 0;
            $("#codeInfo").empty();
            for (var j = 0; j < codesInfo.length; j++) {
                var info = codesInfo[j];
                for (var i = 0; i < result.length; i++) {
                    var item = result[i];
                    var code = item.STATUSCODE;
                    //console.log(code + '==' + info.code + '=' + (code == info.code));
                    if (code == info.code) {
                        var html = [];
                        html.push('<div class="info-item">');
                        html.push('<a class="item" ' + (info.href ? 'href="' + getPath() + info.href + '"' : 'style="cursor: default;"') + '>');
                        html.push('<div class="item-icon ' + info.cssClass + '"></div>');
                        html.push('<div class="item-text">');
                        html.push('<h4>' + item.DOSSIERCOUNT + '个</h4>');
                        html.push('<h5>' + info.label + '</h5>');
                        html.push('</div></a></div>');
                        $("#codeInfo").append(html.join(""));
                        lastIndex++;
                        if (lastIndex > maxLen)
                            return;
                        break;
                    }
                }
            }
        });
    }

    /**
     * 初始化周柱状图
     */
    function initWeekChart() {
        var mergeOnTime = true;//是否合并已做和按时指标
        var myChart = echarts.init(document.getElementById("week-content"));
        post(getPath() + "evaluate/job/getJobEvaluation", {period: "WEEK"}, function (data) {
            var result = eval(data);
            var xAxisData = [];
            var seriesData = [];
            var overTimeData = [];
            var match;
            $.each(result.standardList, function (key, standard) {
                if (standard.sequ <= 0) return true;
                if (standard.queryType !== "CALC") {
                    match = false;
                    if (mergeOnTime && (standard.queryType === "DONE" || standard.queryType === "ONTIME")) {
                        $.each(result.standardList, function (i, item) {
                            if (item.statusCode === standard.statusCode && (item.queryType === "DONE" || item.queryType === "ONTIME") && item.id !== standard.id) {
                                if (standard.queryType === "DONE") {
                                    xAxisData.push(standard.name);
                                    seriesData.push(result.statisList[0][item.id]);
                                    overTimeData.push(result.statisList[0][standard.id] - result.statisList[0][item.id]);
                                } else {
                                    xAxisData.push(item.name);
                                    seriesData.push(result.statisList[0][standard.id]);
                                    overTimeData.push(result.statisList[0][item.id] - result.statisList[0][standard.id]);
                                }
                                item.sequ = 0;
                                match = true;
                                return false;
                            }
                        });
                    }
                    if (match) return true;
                    xAxisData.push(standard.name);
                    seriesData.push(result.statisList[0][standard.id]);
                    overTimeData.push(null);
                }
            });
            var option = {
                color: ["#3296dc"],
                tooltip: {
                    trigger: "axis",
                    formatter: mergeOnTime ? function (params) {
                        var tar0 = params[0];
                        var tar1 = params[1];
                        if (tar1 != null && tar1.value != null)
                            return tar0.name + ": " + (tar0.value + tar1.value)
                                + "<br/><span class='tooltip-circle' style='background-color:" + tar0.color + "'></span>按时: " + tar0.value
                                + "<br/><span class='tooltip-circle' style='background-color:" + tar1.color + "'></span>超时: " + tar1.value;
                        else
                            return tar0.name + "<br/>" + "<span class='tooltip-circle' style='background-color:" + tar0.color + "'></span>" + tar0.value;
                    } : null
                },
                grid: {
                    top: 20,
                    left: 10,
                    right: 10,
                    bottom: 10,
                    containLabel: true
                },
                xAxis: {
                    type: "category",
                    data: xAxisData,
                    axisLabel: {
                        interval: 0
                    }
                },
                yAxis: {
                    max: maxNumber > 0 ? maxNumber : null
                },
                series: [{
                    type: "bar",
                    barWidth: "60%",
                    stack: "总数量",
                    data: seriesData
                }, {
                    type: "bar",
                    barWidth: "60%",
                    stack: "总数量",
                    itemStyle: {
                        normal: {
                            color: '#aaa'
                        }
                    },
                    data: overTimeData
                }]
            };
            myChart.setOption(option);
            changeCSS("week-content");
        });
    }

    /**
     * 初始化月柱状图
     */
    function initMonthChart() {
        var mergeOnTime = true;//是否合并已做和按时指标
        var myChart = echarts.init(document.getElementById("month-content"));
        post(getPath() + "evaluate/job/getJobEvaluation", {period: "MONTH"}, function (data) {
            var result = eval(data);
            var xAxisData = [];
            var seriesData = [];
            var overTimeData = [];
            var pieData = [];
            var match;
            $.each(result.standardList, function (key, standard) {
                if (standard.sequ <= 0) return true;
                if (standard.queryType !== "CALC") {
                    match = false;
                    if (mergeOnTime && (standard.queryType === "DONE" || standard.queryType === "ONTIME")) {
                        $.each(result.standardList, function (i, item) {
                            if (item.statusCode === standard.statusCode && (item.queryType === "DONE" || item.queryType === "ONTIME") && item.id !== standard.id) {
                                if (standard.queryType === "DONE") {
                                    xAxisData.push(standard.name);
                                    seriesData.push(result.statisList[0][item.id]);
                                    overTimeData.push(result.statisList[0][standard.id] - result.statisList[0][item.id]);
                                } else {
                                    xAxisData.push(item.name);
                                    seriesData.push(result.statisList[0][standard.id]);
                                    overTimeData.push(result.statisList[0][item.id] - result.statisList[0][standard.id]);
                                    if (result.statisList[0][item.id] > maxNumber) maxNumber = result.statisList[0][item.id];
                                }
                                item.sequ = 0;
                                match = true;
                                return false;
                            }
                        });
                    }
                    if (result.statisList[0][standard.id] > maxNumber) maxNumber = result.statisList[0][standard.id];
                    if (match) return true;
                    xAxisData.push(standard.name);
                    seriesData.push(result.statisList[0][standard.id]);
                    overTimeData.push(null);
                } else if (standard.statusCode.indexOf("OVERALL") === -1) {
                    pieData.push({name: standard.name, value: result.statisList[0][standard.id]});
                }
            });
            maxNumber = Math.ceil(maxNumber / 5) * 5;//取整5
            var option = {
                color: ["#dc3c28"],
                tooltip: {
                    trigger: "axis",
                    formatter: mergeOnTime ? function (params) {
                        var tar0 = params[0];
                        var tar1 = params[1];
                        if (tar1 != null && tar1.value != null)
                            return tar0.name + ": " + (tar0.value + tar1.value)
                                + "<br/><span class='tooltip-circle' style='background-color:" + tar0.color + "'></span>按时: " + tar0.value
                                + "<br/><span class='tooltip-circle' style='background-color:" + tar1.color + "'></span>超时: " + tar1.value;
                        else
                            return tar0.name + "<br/>" + "<span class='tooltip-circle' style='background-color:" + tar0.color + "'></span>" + tar0.value;
                    } : null
                },
                grid: {
                    top: 20,
                    left: 10,
                    right: 10,
                    bottom: 10,
                    containLabel: true
                },
                xAxis: {
                    type: "category",
                    data: xAxisData,
                    axisLabel: {
                        interval: 0
                    }
                },
                yAxis: {
                    max: maxNumber > 0 ? maxNumber : null
                },
                series: [{
                    type: "bar",
                    barWidth: "60%",
                    stack: "总数量",
                    data: seriesData
                }, {
                    type: "bar",
                    barWidth: "60%",
                    stack: "总数量",
                    itemStyle: {
                        normal: {
                            color: '#aaa'
                        }
                    },
                    data: overTimeData
                }]
            };
            myChart.setOption(option);
            changeCSS("month-content");
            initWeekChart();//先月再周,保持刻度一致
            createPieChart(pieData);
        });
    }

    /**
     * 初始化年柱状图
     */
    function initYearChart() {
        var myChart = echarts.init(document.getElementById("year-content"));
        post(getPath() + "evaluate/job/getJobEvaluation", {period: "YEAR"}, function (data) {
            var result = eval(data);
            var legendData = [];
            var xAxisData = [];
            var seriesData = [];
            $.each(result.statisList, function (key, value) {
                xAxisData.push(key + "月");
            });
            $.each(result.standardList, function (key, standard) {
                if (this.queryType !== "CALC") {
                    legendData.push(standard.name);
                    var object = {name: standard.name, type: "bar", data: []};
                    $.each(result.statisList, function (statisKey, statisList) {
                        object.data.push(statisList[0][standard.id]);
                    });
                    seriesData.push(object);
                }
            });
            var option = {
                tooltip: {
                    trigger: "axis"
                },
                grid: {
                    top: 20,
                    left: 10,
                    right: 10,
                    bottom: 10,
                    containLabel: true
                },
                legend: {
                    data: legendData
                },
                xAxis: {
                    type: "category",
                    data: xAxisData
                },
                yAxis: {},
                series: seriesData
            };
            myChart.setOption(option);
            changeCSS("year-content");
        });
    }

    /**
     * 创建饼图
     */
    function createPieChart(pieData) {
        var xNumber = 2;//饼图x轴排列数量
        var yNumber = 3;//饼图y轴排列数量
        //自动调整排列数量
        var number = Math.ceil(Math.sqrt(pieData.length));
        if (pieData.length <= number * (number - 1)) {
            xNumber = number - 1;
            yNumber = number;
        } else xNumber = yNumber = number;
        var myChart = echarts.init(document.getElementById("percent-content"));
        var seriesData = [];
        var radius = 90 / Math.max(xNumber, yNumber);
        $.each(pieData, function (key, data) {
            if (key >= xNumber * yNumber) return false;//超过数量不显示
            var xCenter = (50 + (key % xNumber) * 100) / xNumber;
            var yCenter = (50 + (Math.floor(key / xNumber) % yNumber) * 100) / yNumber;
            var object = {
                type: "pie",
                center: [xCenter + "%", yCenter + "%"],
                radius: [(radius - 2) + "%", radius + "%"],
                label: {
                    normal: {
                        position: "center"
                    }
                },
                silent: true,
                data: [{
                    value: data.value === "--" ? 0 : data.value,
                    label: {
                        normal: {
                            formatter: data.value === "--" ? "--" : "{d} %",
                            textStyle: {
                                fontSize: 20,
                                fontWeight: "bold"
                            }
                        }
                    }
                }, {
                    value: data.value === "--" ? 1 : 1 - data.value,
                    label: {
                        normal: {
                            formatter: "\n月" + (data.name.length <= 5 ? data.name : data.name.substring(0, data.name.length - 3) + "\n" + data.name.substring(data.name.length - 3)),
                            textStyle: {
                                color: "#555",
                                fontSize: 14
                            }
                        }
                    },
                    itemStyle: {
                        normal: {
                            color: "#aaa"
                        }
                    }
                }]
            };
            seriesData.push(object);
        });
        var option = {
            color: ["#dc3c28"],
            series: seriesData
        };
        myChart.setOption(option);
        changeCSS("percent-content");
    }

    /**
     * 将透明背景替换为白色
     * @param div
     */
    function changeCSS(div) {
        $("#" + div).css("background", "#fff").css("position", "initial");
    }
})();
